package com.pengke.pangu.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pengke.pangu.common.utils.R;
import com.pengke.pangu.service.sys.entity.SysLog;
import com.pengke.pangu.service.sys.service.ISysLogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sysLog")
public class SysLogController {

    @Resource
    private ISysLogService sysLogService;

    @GetMapping("/getSysLogList")
    @SaCheckPermission("sys:log:read")
    public R getSysLogList(SysLog sysLog, String startTime, String endTime, Page<SysLog> page){
        Map<String,Object> map = new HashMap<>();
        List<SysLog> list = sysLogService.list(page, Wrappers.<SysLog>query().lambda()
                .eq(ObjectUtil.isNotEmpty(sysLog.getId()),SysLog::getId,sysLog.getId())
                .eq(ObjectUtil.isNotEmpty(sysLog.getModel()),SysLog::getModel,sysLog.getModel())
                .eq(ObjectUtil.isNotEmpty(sysLog.getBizType()),SysLog::getBizType,sysLog.getBizType())
                .eq(ObjectUtil.isNotEmpty(sysLog.getMethodName()),SysLog::getMethodName,sysLog.getMethodName())
                .eq(ObjectUtil.isNotEmpty(sysLog.getMethodType()),SysLog::getMethodType,sysLog.getMethodType())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpUserId()),SysLog::getOpUserId,sysLog.getOpUserId())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpUserName()),SysLog::getOpUserName,sysLog.getOpUserName())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpIp()),SysLog::getOpIp,sysLog.getOpIp())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpUrl()),SysLog::getOpUrl,sysLog.getOpUrl())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpStatus()),SysLog::getOpStatus,sysLog.getOpStatus())
                .ge(ObjectUtil.isNotEmpty(startTime),SysLog::getOpDatetime,startTime)
                .le(ObjectUtil.isNotEmpty(endTime),SysLog::getOpDatetime,endTime)
                .orderByDesc(SysLog::getId)
        );
        Long total = sysLogService.count(Wrappers.<SysLog>query().lambda()
                .eq(ObjectUtil.isNotEmpty(sysLog.getId()),SysLog::getId,sysLog.getId())
                .eq(ObjectUtil.isNotEmpty(sysLog.getModel()),SysLog::getModel,sysLog.getModel())
                .eq(ObjectUtil.isNotEmpty(sysLog.getBizType()),SysLog::getBizType,sysLog.getBizType())
                .eq(ObjectUtil.isNotEmpty(sysLog.getMethodName()),SysLog::getMethodName,sysLog.getMethodName())
                .eq(ObjectUtil.isNotEmpty(sysLog.getMethodType()),SysLog::getMethodType,sysLog.getMethodType())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpUserId()),SysLog::getOpUserId,sysLog.getOpUserId())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpUserName()),SysLog::getOpUserName,sysLog.getOpUserName())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpIp()),SysLog::getOpIp,sysLog.getOpIp())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpUrl()),SysLog::getOpUrl,sysLog.getOpUrl())
                .eq(ObjectUtil.isNotEmpty(sysLog.getOpStatus()),SysLog::getOpStatus,sysLog.getOpStatus())
                .ge(ObjectUtil.isNotEmpty(startTime),SysLog::getOpDatetime,startTime)
                .le(ObjectUtil.isNotEmpty(endTime),SysLog::getOpDatetime,endTime)
        );

        map.put("list",list);
        map.put("total",total);
        return R.ok(map);
    }
}
